package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neusoft.EYSystem.dao.MealScheduleDAO;
import com.neusoft.EYSystem.dao.MealitemDAO;
import com.neusoft.EYSystem.dao.MealpickDAO;
import com.neusoft.EYSystem.po.*;
import com.neusoft.EYSystem.service.MealScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mealSchedule")
public class MealScheduleController {
    //注 pick的status 2：启用膳食日历 3：不启用膳食日历
    @Autowired
    private MealScheduleService mealScheduleService;
    @Autowired
    private MealScheduleDAO mealScheduleDAO;
    @Autowired
    private MealpickDAO mealpickDAO;
    @Autowired
    private MealitemDAO mealitemDAO;

    //管理员端：膳食管理 膳食日历管理 显示膳食日历表
    //支持根据膳食名、星期、餐次搜索
    @GetMapping("/pageMealSchedule")
    public PageResponseBean<List<MealScheduleAndItemAndPick>> pageMealSchedule(Long cur, Long number, MealSchedule mealSchedule,Mealitem mealitem) {
        return mealScheduleService.pageMealSchedule(cur, number, mealSchedule, mealitem);
    }

    //健康管家端： 膳食管理 膳食日历 查看膳食日历 不显示状态未启用日历 即只查询status为2
    @GetMapping("/pageMealScheduleByStatus")
    public PageResponseBean<List<MealScheduleAndItemAndPick>> pageMealScheduleByStatus(Long cur, Long number, MealSchedule mealSchedule,Mealitem mealitem) {
        return mealScheduleService.pageMealScheduleBystuts(cur, number, mealSchedule, mealitem);
    }
    //管理员端：膳食管理 膳食日历管理 添加膳食日历
    // 注 sql语法写死膳食日历数据库
    @PostMapping("/addMealSchedule")
    public ResponseBean<Integer> addMealSchedule(MealSchedule mealSchedule,Mealitem mealitem) {
        return mealScheduleService.addMealSchedule(mealSchedule,mealitem);
    }
    //管理员端：膳食管理 膳食日历管理 删除膳食日历
    @PostMapping("/deleteMealSchedule")
    public ResponseBean<Integer> deleteMealSchedule(Mealpick mealpick) {
        Integer result;
        ResponseBean<Integer> rb = null;
        UpdateWrapper<Mealpick> uw = new UpdateWrapper<>();
        uw.eq("mp_id", mealpick.getMpId());
        mealpick.setStatus("3");
        try{
            result = mealpickDAO.update(mealpick, uw);
            rb = new ResponseBean<>(result);
        }catch (Exception e){
            e.printStackTrace();
            rb =new ResponseBean<>(500,"删除膳食日历错误");
        }
        return rb;
    }
    //管理员端：膳食管理 膳食日历管理 修改膳食日历
    @PostMapping("/updateMealSchedule")
    public ResponseBean<Integer> updateMealSchedule(Mealpick mealpick,Mealitem mealitem) {
        Integer result;
        ResponseBean<Integer> rb = null;
        UpdateWrapper<Mealpick> uw = new UpdateWrapper<>();
        uw.eq("mp_id", mealpick.getMpId());
        QueryWrapper<Mealitem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", mealitem.getName());
        Mealitem existingMealitem = mealitemDAO.selectOne(queryWrapper);

        mealpick.setMealitemId(existingMealitem.getMealitemId());
        try{
            result = mealpickDAO.update(mealpick, uw);
            rb = new ResponseBean<>(result);
        }catch (Exception e){
            e.printStackTrace();
            rb =new ResponseBean<>(500,"修改膳食日历错误");
        }
        return rb;
    }
}
